home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XFILEIO.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  8KB  |  311 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XFILEIO
  3. ;
  4. ; Sequential binary file I/O functions
  5. ;
  6. ; Some functions based on a r.g.p post by Joshua Jensen
  7. ;
  8. ; Compile with Tasm.
  9. ; C callable.
  10. ;
  11. ;
  12. ; ****** XLIB - Mode X graphics library                ****************
  13. ; ******                                               ****************
  14. ; ****** Written By Themie Gouthas                     ****************
  15. ; ****** Aeronautical Research Laboratory              ****************
  16. ; ****** Defence Science and Technology Organisation   ****************
  17. ; ****** Australia                                     ****************
  18. ;
  19. ; egg@dstos3.dsto.gov.au
  20. ; teg@bart.dsto.gov.au
  21. ;-----------------------------------------------------------------------
  22. COMMENT $
  23.  
  24.  
  25. $
  26.  
  27. LOCALS
  28. .286
  29.  
  30. include model.inc
  31. include xfileio.inc
  32.  
  33.     .code
  34.  
  35. PUSH_DS macro
  36.     IFNDEF s
  37.     push  ds
  38.     ENDIF
  39.     endm
  40.  
  41. POP_DS macro
  42.     IFNDEF s
  43.     pop   ds
  44.     ENDIF
  45.     endm
  46.  
  47. LDS_M macro arg1,arg2
  48.     IFNDEF s
  49.     lds &arg1&,&arg2&
  50.     ELSE
  51.     mov &arg1&,word ptr &arg2&
  52.     ENDIF
  53.     endm
  54.  
  55.  
  56.  
  57. ;****************************************************************
  58. ;
  59. ; name: f_open
  60. ;
  61. ; C Prototype:
  62. ;
  63. ;     extern int f_open(char * filename, char access)
  64. ;
  65. ; Opens a file according to the access char:
  66. ;
  67. ;   0 = read only   - If doesnt exist return error
  68. ;   1 = write only  - If doesnt exist create it otherwise clear it
  69. ;   2 = read/write  - If doesnt exist create it
  70. ;
  71. ; Returns the file handle on success, -1 on failure
  72. ;
  73. ;
  74. proc _f_open
  75. IFNDEF s
  76.   ARG   filename:dword,access:byte
  77. ELSE
  78.   ARG   filename:word,access:byte
  79. ENDIF
  80.     push  bp             ; Preserve caller's stack frame
  81.     mov   bp,sp
  82.     PUSH_DS
  83.     LDS_M dx,[filename]  ; point DS:DX to file name string
  84.         cmp   [access],1
  85.     je    @@creat
  86.  
  87.     mov   ah,3dh         ; select "open file" DOS service
  88.     mov   al,[access]    ; select access type code
  89.     int   21h            ; call DOS service
  90.     jnb   @@Done         ; If carry flag set we have failed
  91.  
  92.  
  93.     cmp   [access],2
  94.     jne   @@error
  95. @@creat:
  96.     mov   ah,3ch         ; select "creat file" DOS service
  97.     mov   cx,0
  98.     int   21h            ; call DOS service
  99.     jnb   @@Done         ; If carry flag set we have failed
  100. @@error:
  101.     mov   ax,-1          ;  indicate failure
  102. @@Done:                      ; otherwise return file handle
  103.     POP_DS
  104.     pop  bp              ;restore caller's stack frame
  105.     ret
  106. _f_open endp
  107.  
  108.  
  109. ;****************************************************************
  110. ;
  111. ; name: f_close
  112. ;
  113. ; C Prototype:
  114. ;
  115. ;     extern int f_close(int handle)
  116. ;
  117. ; Closes the file associated with the specified handle
  118. ;
  119. ; Returns 0 on success, -1 on failure
  120. ;
  121. proc _f_close
  122. ARG   handle:word
  123.     push bp             ; Preserve caller's stack frame
  124.     mov  bp,sp
  125.  
  126.     mov  ah,3eh         ; select  "close file handle" DOS service
  127.     mov  bx,[handle]    ; select handle of file to close
  128.     int  21h            ; call DOS service
  129.     jnb  @@Fix          ; failed if carry flag set
  130.     mov  ax,-1          ;  return error
  131.     jmp  short @@Done
  132. @@Fix:                      ; otherwise
  133.     xor  ax,ax          ;  return 0
  134. @@Done:
  135.     pop  bp             ;restore caller's stack frame
  136.     ret
  137. _f_close endp
  138.  
  139.  
  140. ;****************************************************************
  141. ;
  142. ; name: f_read
  143. ;
  144. ; C Prototype:
  145. ;
  146. ;     extern int f_read(int handle, char far * buffer, int count)
  147. ;
  148. ; Reads a block of count bytes from the file specified by the handle
  149. ; into the buffer
  150. ;
  151. ; Returns count on success, -1 on failure
  152. ;
  153. proc _f_read
  154. ARG   handle:word,buffer:dword,count:word
  155.     push bp             ; Preserve caller's stack frame
  156.     mov  bp,sp
  157.     push ds
  158.  
  159.     mov  ah,3fh         ; select "read from file or device" DOS service
  160.     mov  bx,[handle]    ; select handle of file to close
  161.     mov  cx,[count]
  162.     lds  dx,[buffer]
  163.     int  21h            ; call DOS service
  164.     jnb  @@Fix          ; failed if carry flag set
  165.     mov  ax,-1          ;  return error
  166.     jmp  short @@Done
  167. @@Fix:                      ; otherwise
  168.     xor  ax,ax          ;  return 0
  169. @@Done:
  170.     pop  ds
  171.     pop  bp             ;restore caller's stack frame
  172.     ret
  173. _f_read endp
  174.  
  175. ;****************************************************************
  176. ;
  177. ; name: f_write
  178. ;
  179. ; C Prototype:
  180. ;
  181. ;     extern int f_write(int handle, char far * buffer, int count)
  182. ;
  183. ; Writes a block of count bytes to the file specified by the handle
  184. ; from the buffer
  185. ;
  186. ; Returns count on success, -1 on failure
  187. ;
  188. proc _f_write
  189. ARG   handle:word,buffer:dword,count:word
  190.     push bp             ; Preserve caller's stack frame
  191.     mov  bp,sp
  192.     push ds
  193.  
  194.     mov  ah,40h         ; select "write to file or device" DOS service
  195.     mov  bx,[handle]    ; select handle of file to write
  196.     mov  cx,[count]
  197.     lds  dx,[buffer]
  198.     int  21h            ; call DOS service
  199.     jnb  @@Done         ; has the function failed ?
  200.     mov  ax,-1          ;  yes, return error
  201.     jmp  short @@Done
  202. @@Done:                     ; otherwise return bytes written
  203.     pop  ds
  204.     pop  bp             ; restore caller's stack frame
  205.     ret
  206. _f_write endp
  207.  
  208. ;****************************************************************
  209. ;
  210. ; name: f_seek
  211. ;
  212. ; C Prototype:
  213. ;
  214. ;   extern long int f_seek(int handle, long int position, char method_code)
  215. ;
  216. ; Moves the file pointer according to the position and method code
  217. ;
  218. ; Returns file pointer position on success, -1 on failure
  219. ;
  220. proc _f_seek
  221. ARG   handle:word,position:dword,method_code:byte
  222.     push bp             ; Preserve caller's stack frame
  223.     mov  bp,sp
  224.  
  225.     mov  ah,42h         ; select "move file pointer" DOS service
  226.     mov  bx,[handle]    ; select handle of file to close
  227.     mov  al,[method_code]
  228.     mov  cx,word ptr [position+2]
  229.     mov  dx,word ptr [position]
  230.     int  21h            ; call DOS service
  231.     jnb  @@Done         ; has the function failed ?
  232.     mov  ax,-1          ;  yes, return error
  233.     mov  dx,-1          ;
  234.     jmp  short @@Done
  235. @@Done:                     ; otherwise return bytes written
  236.     pop  bp             ; restore caller's stack frame
  237.     ret
  238. _f_seek endp
  239.  
  240.  
  241. ;****************************************************************
  242. ;
  243. ; name: f_filelength
  244. ;
  245. ; C Prototype:
  246. ;
  247. ;   extern long int f_filelength(int handle)
  248. ;
  249. ; Returns the length of the file associated with the specified handle
  250. ;
  251. ; Returns file length on success, -1 on failure
  252. ;
  253. proc _f_filelength
  254. ARG     handle:word
  255. LOCAL   low:word,high:word=LocalStk
  256.     push bp             ; Preserve caller's stack frame
  257.     mov  bp,sp
  258.     sub  sp,LocalStk
  259.  
  260.     ; Get ptr's current location in file and save it
  261.  
  262.     mov  ah,42h         ; select "move file pointer" DOS service
  263.     mov  al,1           ; select "from current location" method
  264.     mov  bx,[handle]    ; select handle of file to close
  265.     xor  cx,cx
  266.     xor  dx,dx
  267.     int  21h
  268.     jb   @@Error
  269.     mov  [low],ax
  270.     mov  [high],dx
  271.  
  272.     ; Get ptr's value at end of file
  273.  
  274.     mov  ah,42h         ; select "move file pointer" DOS service
  275.     mov  al,2           ; select "from end of file" method
  276.         mov  bx,[handle]    ; select handle of file to close
  277.     xor  cx,cx
  278.     xor  dx,dx
  279.     int  21h
  280.     jb   @@Error
  281.  
  282.     ; Save the results while returning pointer to its previous location
  283.  
  284.     push ax
  285.     push dx
  286.  
  287.         mov  ah,42h         ; select "move file pointer" DOS service
  288.     mov  al,0           ; select "from start of file" method
  289.         mov  bx,[handle]    ; select handle of file to close
  290.     mov  cx,[high]
  291.     mov  dx,[low]
  292.     int  21h
  293.  
  294.     ; restore resultant length
  295.  
  296.     pop  dx
  297.     pop  ax
  298.     jnb   @@Done        ; Was the operation a success ?
  299. @@Error:
  300.     mov  ax,-1          ;  no, return error
  301.     mov  dx,-1          ;
  302. @@Done:                     ; otherwise return bytes written
  303.     mov  sp,bp
  304.     pop  bp             ; restore caller's stack frame
  305.     ret
  306. _f_filelength endp
  307.  
  308.  
  309.     end
  310.  
  311.